package com.chunxiadong.controller.admin;

import com.chunxiadong.entity.Avatar;
import com.chunxiadong.entity.Picture;
import com.chunxiadong.service.AdminService;
import com.chunxiadong.service.PictureService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @auther QiuShangcheng
 * @create 2021/6/8
 */
@Controller
@RequestMapping("/admin/picture")
public class PictureController {
    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    PictureService pictureService;

    @Autowired
    AdminService adminService;

    /**
     * 跳转到添加图片页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String inputAddPicture(Model model, HttpSession session) {
        model.addAttribute("picture", new Picture());
        if (session.getAttribute("pictureAddr") == null){
            session.setAttribute("pictureAddr","If Want To Upload Local Picture, Please Firstly Upload Picture!");
        }
        return "admin/picture-input";
    }

    /**
     * 跳转到修改页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/inputUpdatePicture/{id}")
    public String inputUpdatePicture(@PathVariable Integer id, Model model){
        model.addAttribute("picture", pictureService.queryPictureById(id));
        return "admin/picture-input";
    }

    /**
     * 删除照片
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/deletePicture/{id}")
    public String deletePicture(@PathVariable Integer id,RedirectAttributes model){
        int i = pictureService.deletePicture(id);
        if (i == 0) {
            model.addFlashAttribute("picture_message", " Delete_Picture Case Error!");
            log.info("Result : {" + "Delete_Picture Case Error!" + "}");
        } else {
            model.addFlashAttribute("picture_message", "Delete_Picture Successful!");
        }
        return "redirect:/admin/picture";
    }

    /**
     * 更新照片信息
     * @param id
     * @param picture
     * @param attributes
     * @return
     */
    @RequestMapping("/updatePicture/{id}")
    public String updatePicture(@PathVariable Integer id, Picture picture, RedirectAttributes attributes) {
        Picture queryRes = pictureService.queryPictureById(id);
        if (queryRes == null) {
            attributes.addFlashAttribute("picture_message", "No Picture_Data!");
            log.info(picture.toString() + "\nResult : {" + " No Picture_Data!" + "}");
            return "redirect:/admin/picture";
        }
        int i = pictureService.updatePicture(picture);
        if (i == 0) {
            attributes.addFlashAttribute("picture_message", " Update_Picture Case Error!");
            log.info(picture.toString() + "\nResult : {" + " Update_Picture Case Error!" + "}");
        }
        attributes.addFlashAttribute("picture_message", "Update_Picture Successful!");
        return "redirect:/admin/picture";
    }


    /**
     * 添加照片
     *
     * @param picture
     * @param attributes
     * @return
     */
    @RequestMapping("/addPicture")
    public String addPicture(Picture picture, RedirectAttributes attributes) {
        Picture queryRes = pictureService.queryPictureByPicAddr(picture.getPictureAddr());
        if (queryRes != null) {
            attributes.addFlashAttribute("picture_message", "Do not add picture repeatedly!");
            log.info(picture.toString() + "\nResult : {" + " Add_Picture repeatedly!" + "}");
            return "redirect:/admin/picture";
        }
        int i = pictureService.addPicture(picture);
        if (i == 0) {
            attributes.addFlashAttribute("picture_message", " Add_Picture Case Error!");
            log.info(picture.toString() + "\nResult : {" + " Add_Picture Case Error!" + "}");
        }
        attributes.addFlashAttribute("picture_message", "Add_Picture Successful!");
        return "redirect:/admin/picture";
    }

    /**
     * 设置头像
     * @param id
     * @param pictureAddr
     * @param attributes
     * @return
     */
    @RequestMapping("/setAvatar/{id}")
    public String setAvatar(@PathVariable Integer id, String pictureAddr,RedirectAttributes attributes,HttpSession session){
        Avatar avatar = new Avatar(id, pictureAddr);
        int i = pictureService.setAvatar(avatar);
        if (i == 0){
            attributes.addFlashAttribute("picture_message", " Add_avatar Case Error!");
            log.info(avatar.toString() + "\nResult : {" + " Add_Avatar Case Error!" + "}");
        }
        attributes.addFlashAttribute("picture_message", "Add_Avatar Successful!");

        session.setAttribute("user",adminService.getAdminById(id));
        return "redirect:/admin/picture";
    }
}
